{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# I- Demo. Data Access"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "This notebook aims at documenting how to access & manipulate the input datasets for one \"ocean data challenge\".\n",
    "Two *tar.gz* archives with Sea Surface Height (SSH) datasets are available on the MEOM opendap server.\n",
    "The **dc_ref** dataset refers to the reference simulation, a.k.a NATL60-CMJ165 nature run carried out by the MEOM Team. The **dc_obs** corresponds to the observations datasets (for various altimeter missions) based on nadir (TOPEX/Poseidon, Jason1, Envisat, Geosat-2) and large swath (SWOT) orbits constructed with the [SWOTsimulator](https://github.com/SWOTsimulator/swotsimulator) package."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "import xarray as xr\n",
    "import hvplot.xarray\n",
    "import sys"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "sys.path.append('..')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from src.mod_plot import *"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "# Get pseuso-observation\n",
    "!wget https://ige-meom-opendap.univ-grenoble-alpes.fr/thredds/fileServer/meomopendap/extract/ocean-data-challenges/dc_data1/dc_obs.tar.gz"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "!tar -xvf dc_obs.tar.gz --directory ../"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "!rm -f dc_obs.tar.gz"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### Load Jason1 pseudo obs."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "ds_j1 = xr.open_dataset('../dc_obs/2020a_SSH_mapping_NATL60_jason1.nc')\n",
    "ds_j1"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### Load Envisat pseudo obs."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "ds_en = xr.open_dataset('../dc_obs/2020a_SSH_mapping_NATL60_envisat.nc')\n",
    "ds_en"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### Load Topex/Poseidon Interleaved pseudo obs."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "scrolled": true
   },
   "outputs": [],
   "source": [
    "ds_tpn = xr.open_dataset('../dc_obs/2020a_SSH_mapping_NATL60_topex-poseidon_interleaved.nc')\n",
    "ds_tpn"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### Load Geosat2 pseudo obs."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "ds_g2 = xr.open_dataset('../dc_obs/2020a_SSH_mapping_NATL60_geosat2.nc')\n",
    "ds_g2"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### Load SWOT nadir pseudo obs."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "ds_swot_nadir = xr.open_dataset('../dc_obs/2020a_SSH_mapping_NATL60_nadir_swot.nc')\n",
    "ds_swot_nadir"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### Load SWOT karin pseudo obs."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "ds_swot_karin = xr.open_dataset('../dc_obs/2020a_SSH_mapping_NATL60_karin_swot.nc')\n",
    "ds_swot_karin"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "# Get nature run (it may take several minutes depending on your connection!!!!)\n",
    "!wget https://ige-meom-opendap.univ-grenoble-alpes.fr/thredds/fileServer/meomopendap/extract/ocean-data-challenges/dc_data1/dc_ref.tar.gz"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "!tar -xvf dc_ref.tar.gz --directory ../"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "!rm -f dc_ref.tar.gz"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "dc_ref = xr.open_mfdataset('../dc_ref/*.nc', combine='nested', concat_dim='time')\n",
    "dc_ref"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### Example of figures"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "list_of_dataset = [ds_j1, ds_g2, ds_en, ds_tpn]\n",
    "central_date = numpy.datetime64('2012-10-05')\n",
    "delta_t = numpy.timedelta64(5, 'D')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "plot_demo_obs(list_of_dataset, dc_ref, central_date, delta_t)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "list_of_dataset = [ds_swot_karin]\n",
    "central_date = numpy.datetime64('2012-10-05')\n",
    "delta_t = numpy.timedelta64(5, 'D')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "plot_demo_obs(list_of_dataset, dc_ref, central_date, delta_t)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### Example of \"interactive\" figures"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "list_of_dataset = [ds_j1, ds_g2, ds_en, ds_tpn]\n",
    "central_date = numpy.datetime64('2012-10-05')\n",
    "delta_t = numpy.timedelta64(5, 'D')\n",
    "hvplot_demo_obs_nadir(list_of_dataset, dc_ref, central_date, delta_t)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "list_of_dataset = ds_swot_karin\n",
    "central_date = numpy.datetime64('2012-10-05')\n",
    "delta_t = numpy.timedelta64(5, 'D')\n",
    "hvplot_demo_obs_karin(list_of_dataset, dc_ref, central_date, delta_t)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.7.7"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 4
}
